package com.example.demo.config;

import com.example.demo.ecrypt.EncryptMessageConverter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.io.*;

@Slf4j
@Configuration
public class HttpMessageConverterConfig {


    //引入Fastjson解析json，不使用默认的jackson
    //必须在pom.xml引入fastjson的jar包，并且版必须大于1.2.10
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {

//        //4、将convert添加到converters中
//        HttpMessageConverter converter = new HttpMessageConverter() {
//            @Override
//            public boolean canRead(Class clazz, MediaType mediaType) {
//                return true;
//            }
//
//            @Override
//            public boolean canWrite(Class clazz, MediaType mediaType) {
//                return true;
//            }
//
//            @Override
//            public List<MediaType> getSupportedMediaTypes() {
//                ArrayList<MediaType> mediaTypes = new ArrayList<>();
//                mediaTypes.add(MediaType.ALL);
//                return mediaTypes;
//            }
//
//            @Override
//            public Object read(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
//
//                System.out.println(HttpMessageConverterConfig.inputStream2String(inputMessage.getBody()));
//                Object o = null;
//                try {
//                    o = clazz.newInstance();
//                } catch (InstantiationException e) {
//                    e.printStackTrace();
//                } catch (IllegalAccessException e) {
//                    e.printStackTrace();
//                }
//                return o;
//            }
//
//            @Override
//            public void write(Object o, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
//
//            }
//        };
        EncryptMessageConverter converter = new EncryptMessageConverter();
        //        builder.serializationInclusion(JsonInclude.Include.NON_NULL);
//        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
////        builder.serializationInclusion(JsonInclude.Include.NON_NULL);
//        builder.serializationInclusion(JsonInclude.Include.ALWAYS);
//        builder.deserializerByType(Enum.class, new JsonDeserializer<Enum>() {
//            @Override
//            public Enum deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
//                return null;
//            }
//        });
//        ObjectMapper objectMapper = builder.build();
//        SimpleModule simpleModule = new SimpleModule();
////        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
//        objectMapper.registerModule(simpleModule);
//        objectMapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);// 忽略 transient 修饰的属性
//
//        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(objectMapper);
////        converter.setJsonPrefix("asdfasfasd:sdfasdfasd");
//
////        converter.setPrettyPrint(true);
//
////        converter.set
//


        return new HttpMessageConverters(converter);
    }

}